//
//  CustomLayout.m
//  瀑布流练习2
//
//  Created by mini on 15/7/14.
//  Copyright (c) 2015年 mini. All rights reserved.
//

#import "CustomLayout.h"

@implementation CustomLayout
-(instancetype)init
{
    self = [super init];
    if (self) {
        self.columnMargin = 10;
        self.rowMargin = 10;
        self.count = 2;
        self.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    }
    return self;
}

-(NSMutableDictionary*)maxYDic
{
    if (_maxYDic==nil) {
        _maxYDic = [[NSMutableDictionary alloc]init];
    }
    return _maxYDic;
}

-(NSMutableArray *)array
{
    if (_array == nil) {
        _array = [NSMutableArray array];
    }
    return _array;
}

-(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{
    return YES;
}

-(void)prepareLayout
{
    [super prepareLayout];
    //初始化每列最初的距离
    for (int i = 0; i < _count; i++) {
        NSString *str = [NSString stringWithFormat:@"%d",i];
        self.maxYDic[str] = @(self.sectionInset.top);
    }
    
   //设置每个Item的属性
    [self.array removeAllObjects];
    NSInteger total = [self.collectionView numberOfItemsInSection:0];//获取总数
    //把每一个Item的属性加入到数组中
    for (int i = 0; i < total; i++) {
        UICollectionViewLayoutAttributes *atts = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]];
        [_array addObject:atts];
    }
}

//为每一个Item设置具体的属性
-(UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
     __block NSString *minColumn = @"0";//它用来记录长度最小的列的列号
    [self.maxYDic enumerateKeysAndObjectsUsingBlock:^(NSString *column, NSNumber *maxY , BOOL *stop) {
        if ([maxY floatValue]<[self.maxYDic[minColumn] floatValue]) {
            minColumn = column;
        }
    }];//找到最小的那列的列号
    
    //设置宽和高
    CGFloat width = (self.collectionView.frame.size.width - self.sectionInset.left - self.sectionInset.right - (_count-1)*_rowMargin)/_count;
    
    CGFloat height = [self.delegate waterflowLayout:self heightForWidth:width atIndexPath:indexPath];
    
    
    CGFloat x = self.sectionInset.left + (width + _rowMargin)*[minColumn integerValue];
    
    CGFloat y = [self.maxYDic[minColumn] floatValue] + _columnMargin ;
    //更新最小列的长度
    self.maxYDic[minColumn] = @(height + y);
    
    //设置Item摆放的位置
    UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    
    attrs.frame = CGRectMake(x, y, width, height);
    return attrs;
}

//返回CollectionView的高度
-(CGSize)collectionViewContentSize
{
    __block NSString *maxColumn = @"0";
    [self.maxYDic enumerateKeysAndObjectsUsingBlock:^(NSString *column, NSNumber *maxY , BOOL *stop) {
        if ([maxY floatValue]>[self.maxYDic[maxColumn] floatValue]) {
            maxColumn = column;
        }
    }];
    return CGSizeMake(0, [self.maxYDic[maxColumn] floatValue]+self.sectionInset.bottom);
}

-(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
    return self.array;
}
@end
